package com.atguigu.test.aware;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.*;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

/**
 * @author zp
 * @version 1.0
 * @description: spring aware的使用：
 * 使用了spring Aware 你的bean将会和spring框架耦合，
 * spring aware 的目的是为了让bean获取spring容器的服务：
 *
 * Aware的简单说明：
 * 说清晰一点 就是实现相应的Aware 就是附件spring容器的相关信息，
 * 想包含spring的什么信息包含实现对应的Aware
 *
 * BeanNameAware ：可以获取容器中bean的名称
 * BeanFactoryAware:获取当前bean factory这也可以调用容器的服务
 * ApplicationContextAware： 当前的applicationContext， 这也可以调用容器的服务
 * MessageSourceAware：获得message source，这也可以获得文本信息
 * ApplicationEventPublisherAware：应用事件发布器，可以发布事件，
 * ResourceLoaderAware： 获得资源加载器，可以获得外部资源文件的内容；
 *
 * @date 2022/11/29 19:03
 */
@Component
@Slf4j
public class MyAware implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, MessageSourceAware, ApplicationEventPublisherAware,ResourceLoaderAware {
    private String beanName;
    private BeanFactory beanFactory;
    private ApplicationContext applicationContext;
    private MessageSource messageSource;
    private ApplicationEventPublisher applicationEventPublisher;
    private ResourceLoader resourceLoader;

    public String getBeanName() {
        return beanName;
    }

    public BeanFactory getBeanFactory() {
        return beanFactory;
    }

    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public MessageSource getMessageSource() {
        return messageSource;
    }

    public ApplicationEventPublisher getApplicationEventPublisher() {
        return applicationEventPublisher;
    }

    public ResourceLoader getResourceLoader() {
        return resourceLoader;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        log.info("setBeanFactory 被调用");
        this.beanFactory = beanFactory;
    }

    @Override
    public void setBeanName(String name) {
        log.info("setBeanName 被调用");
        this.beanName = beanName;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        log.info("setApplicationContext 被调用");
        this.applicationContext = applicationContext;
    }

    @Override
    public void setMessageSource(MessageSource messageSource) {
        log.info("setMessageSource 被调用");
        this.messageSource = messageSource;
    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        log.info("applicationEventPublisher 被调用");
        this.applicationEventPublisher = applicationEventPublisher;
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        log.info("setResourceLoader 被调用");
        this.resourceLoader = resourceLoader;
    }
}
